<?php


namespace app\business\model;
use think\Db;
use think\Model;
//商品分类
class CommodityClass extends Model
{
    //查询类型种类（类型名）
    public function classSel(int $businessId)
    {
      $data = $this->table('commodityClass')
            ->field('id,catName,updateTime')
            ->where('businessId','='," $businessId")
          ->where('deleted','=','0')
          ->select();
        return  $data;
    }
    //搜索类型种类（类型名）
    public function classSearch(int $id,string $commodityName){
        // TODO 请使用model类，不使用助手函数
        return $this
            ->table('commodityClass')
            ->where('deleted','=','0')
            ->where('businessId','=',$id)
            ->whereLike('catName',["%$commodityName%"],'and')
            ->field('id,catName,updateTime')
            ->select();
    }
    //添加商品类型
    public function classData(string $type,int $businessId)
    {
        $ClassData = [ //商品类型表
            'id' => '',
            'businessId' => $businessId,//商家编号
            'catName' => $type,//类型名
            'createTime' => time(),
            'updateTime' => time(),
        ];
        return $this->table('commodityClass')->insert($ClassData);
    }    
    public function updataClass(int $classId,string $what,string $value)
    {
        $data=[
            'updateTime' => time(),
            $what => $value
        ];
        return $this->table('commodityClass')
            ->where('id',$classId)
            ->update( $data );
    }
    /**
     * @classification  分类
     * @param int $id
     * @return mixed
     */
    public function classification(int $id){
        return $this->table('commodityClass')->where('id',$id)->value('catName');
    }
    public function findClassificationName(int $businessId,int $catId){
        return $this->table('commodityClass')
            ->field('catName')
            ->where([
            'id'                =>  $catId,
            'businessId'        =>  $businessId
        ])->find();
    }
}


